//
//  ViewController.m
//  Sections
//
//  Created by Justsoso on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>

@property (nonatomic,strong) NSDictionary *namesDict;
@property (nonatomic,strong) NSArray *keys;
@property (weak, nonatomic) IBOutlet UITableView *tabelView;

@property (nonatomic,strong) UISearchController *searchConroller;

@property (nonatomic,strong) NSMutableArray *filterNames;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    self.namesDict = [NSDictionary dictionaryWithContentsOfFile:path];
    self.keys = [[self.namesDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    [self.tabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.tabelView.tag = 1;
    
    self.searchConroller = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchConroller.searchResultsUpdater = self;
    self.searchConroller.dimsBackgroundDuringPresentation = NO;// 是否要变灰
    self.tabelView.tableHeaderView = self.searchConroller.searchBar;
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.searchConroller.active)
    {
        return 1;
    }
    else
    {
        return self.keys.count;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchConroller.active)
    {
        return self.filterNames.count;
    }
    else
    {
        NSString *key = self.keys[section];
        NSArray *arr = self.namesDict[key];
        return arr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (self.searchConroller.active)
    {
        cell.textLabel.text = self.filterNames[indexPath.row];
        return cell;
    }
    else
    {
        NSString *key = self.keys[indexPath.section];
        NSArray *arr = self.namesDict[key];
        cell.textLabel.text = arr[indexPath.row];
        
        return cell;
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.searchConroller.active)
    {
        return nil;
    }
    else
    {
        NSString *key = self.keys[section];
        return key;
    }
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (self.searchConroller.active)
    {
        return nil;
    }
    else
    {
        return self.keys;
    }
    
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    self.filterNames = [NSMutableArray array];
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchString];
    
    for (NSString *key in self.keys)
    {
        NSArray *names = self.namesDict[key];
        NSArray *tmp = [names filteredArrayUsingPredicate:pre];
        
        [self.filterNames addObjectsFromArray:tmp];
    }
    [self.tabelView reloadData];
}



@end
